const express= require('express');
const common = require('../../../libs/common');
const reg = require('../../../libs/reg');
const mail = require('./mail');

let router = express.Router();
module.exports = router;

router.post('/',(req,res)=>{
    let {email,oldPwd,newPwd,rePwd} = req.body;
    // 做效验
    if(!reg.mailReg.test(email)){
        let regData={
            status:2,
            errMsg:"Please enter a valid email address"
        }
        res.send(regData)
    }else if(!reg.pwdReg.test(oldPwd)){
        let regData={
            status:2,
            errMsg:"Enter a combination of letters and Numbers between 5 and 21"
        }
        res.send(regData)
    }else if(!reg.pwdReg.test(newPwd)){
        let regData={
            status:2,
            errMsg:"Enter a combination of letters and numbers between 5 and 21"
        }
        res.send(regData)
    }else if(!reg.pwdReg.test(rePwd)){
        let regData={
            status:2,
            errMsg:"Enter a combination of letters and numbers between 5 and 21"
        }
        res.send(regData)
    }else if(rePwd!=newPwd){
        let regData={
            status:2,
            errMsg:"The password and its confirm are not the same"
        }
        res.send(regData)
    }else if(newPwd == oldPwd){
        let regData={
            status:2,
            errMsg:"The new password does not match the password"
        }
        res.send(regData)
    }else{
        // 判断邮箱验证码是否正确
        if(req.session['shoe-email']!=email){
            let regData={
                status:2, 
                errMsg:"Please change the password of the current account"
            }
            res.send(regData)
        }else{
            req.db.query(`SELECT ID,password FROM user WHERE username='${email}'`, (err, data) => {
                if (err) {
                    res.sendStatus(500);
                }else if(data == 0){
                    let regData={
                        status:2, 
                        errMsg:"Invalid account"
                    }
                    res.send(regData)
                }else if(data[0].ID != req.session['shoe-user_ID']){
                    let regData={
                        status:2, 
                        errMsg:"Please change the password of the current account"
                    }
                    res.send(regData)
                } else if(data[0].password!=common.md5(oldPwd)){
                    let regData={
                        status:2, 
                        errMsg:"The old password was incorrectly entered"
                    }
                    res.send(regData)
                }else{
                    req.db.query(`UPDATE user SET password='${common.md5(newPwd)}' WHERE ID='${data[0].ID}'`,(err,data)=>{
                        if(err){
                            res.sendStatus(500);
                        }else{
                            let regData={
                                status:1
                            }
                            res.send(regData)
                        }
                    })
                }
            })
        }
    }
})
